package com.tmans.adminDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import net.sf.json.JSONObject;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.tmans.common.util.PingPoller;
import com.tmans.common.util.StringUtil;

@Repository
public class AdminServiceImpl implements AdminService {

	@Autowired
	private SqlSession sqlSession;
	
	private static final Logger logger = LoggerFactory.getLogger(AdminServiceImpl.class);
	

		
	public void setSqlSession(SqlSession sqlSession)
    {
        this.sqlSession = sqlSession;
    }
	
		
	@Override		
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor={Exception.class})
	public void insIp(AdminDto adminDto ) throws Exception {
		// TODO Auto-generated method stub
	
		//IP
		adminDto.setIpaddress(adminDto.getIp1()+"."+adminDto.getIp2()+"."+adminDto.getIp3()+"."+adminDto.getIp4());
		
		int rs =  (int)sqlSession.insert("AdminMapper.insIp", adminDto);
		
		logger.debug("*************[[  insIp : insIp]]******result******==>[["+ rs+"]]");
       
	}
	
	@Override
	public int getIpListCnt(AdminDto adminDto ) {
		// TODO Auto-generated method stub
		int totalCnt = (Integer)sqlSession.selectOne("AdminMapper.getIpListCnt",adminDto);
		
		return totalCnt;
	}
	
	@Override
	public List<AdminDto> getIpList(AdminDto adminDto ) {
		// TODO Auto-generated method stub
		List<AdminDto> result = new ArrayList<AdminDto>();
		result = sqlSession.selectList("AdminMapper.getIpList",adminDto);
		
		return result;
	}
	
	@Override
	public int getPcBangCnt(AdminDto adminDto ) {
		// TODO Auto-generated method stub
		int totalCnt = (Integer)sqlSession.selectOne("AdminMapper.getPcBangCnt",adminDto);
		
		return totalCnt;
	}
	
	@Override
	public List<AdminDto> getPcBangList(AdminDto adminDto ) {
		// TODO Auto-generated method stub
		List<AdminDto> result = new ArrayList<AdminDto>();
		result = sqlSession.selectList("AdminMapper.getPcBangList",adminDto);
		
		return result;
	}
	
	@Override
	public int getHourStatsCnt(AdminDto adminDto ) {
		// TODO Auto-generated method stub
		int totalCnt = (Integer)sqlSession.selectOne("AdminMapper.getHourStatsCnt",adminDto);
		
		return totalCnt;
	}
	
	@Override
	public List<AdminDto> getHourStatsList(AdminDto adminDto ) {
		// TODO Auto-generated method stub
		List<AdminDto> result = new ArrayList<AdminDto>();
		result = sqlSession.selectList("AdminMapper.getHourStatsList",adminDto);
		
		return result;
	}
	
	@Override
	public int getArtCnt(AdminDto adminDto ) {
		// TODO Auto-generated method stub
		int totalCnt = (Integer)sqlSession.selectOne("AdminMapper.getArtCnt",adminDto);
		
		return totalCnt;
	}
	
	@Override
	public List<AdminDto> getArtList(AdminDto adminDto ) {
		// TODO Auto-generated method stub
		List<AdminDto> result = new ArrayList<AdminDto>();
		result = sqlSession.selectList("AdminMapper.getArtList",adminDto);
		
		return result;
	}
	
	
	@Override
	public List<HashMap<String, Object>> getBoardCd(String board_cd) {
		// TODO Auto-generated method stub
		
		List<HashMap<String, Object>> outData = new ArrayList<HashMap<String, Object>>();
		
		outData = sqlSession.selectList("AdminMapper.getBoardCd",board_cd);
		
		return outData;
	}

	
	@Override
	public void upBoardDel(AdminDto adminDto) throws Exception {
		// TODO Auto-generated method stub
	
		
					
		/***************************************************************************************************/
		// 게시판 디비 삭제 
		/***************************************************************************************************/
	
		//게시판 삭제 
		sqlSession.delete("AdminMapper.delBoardM", adminDto);
	}
	
	
	
	@Override
	public void scjul() throws Exception {
		// TODO Auto-generated method stub
	
		logger.debug("*************[[  scjul start ]]**************************");
		
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		/***************************************************************************************************/
		// 핑테스트 
		/***************************************************************************************************/
	
		//IP ADRESS
		// TODO Auto-generated method stub
		List<AdminDto> result = new ArrayList<AdminDto>();
		
		
		
		//AdminDto
		AdminDto  valueDto = new AdminDto();
		
		result = sqlSession.selectList("AdminMapper.getIpAdress");
		
		System.out.println("$$$22222222222222222222222$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		
		
		PingPoller pingPoller = new PingPoller();
		
		try {
				for(int i=0; i< result.size();i++){
					
					//핑 테스트
					String receive = pingPoller.pingtest(result.get(i).getIpaddress());
					
					
					valueDto.setReceivest(receive);
					valueDto.setGroupid(result.get(i).getGroupid());
					valueDto.setGroupnm(result.get(i).getGroupnm());
					valueDto.setIpaddress(result.get(i).getIpaddress());
					
					
					//핑 히스토리 저장
					int rs =  (int)sqlSession.insert("AdminMapper.insRecive", valueDto);	  		
			  		logger.debug("*************[[  ping recieve ]]******result******==>[["+ rs+"]]");
					
				}
				
						
			} catch (Exception e) {
				//에러코드 저장
				e.printStackTrace();
			}	
		
	}
	//SSS------------------------------------------------------------------------------------------------------
	
	
	
	@Override		
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor={Exception.class})
	public void upArtDel(AdminDto adminDto ) throws Exception {
		// TODO Auto-generated method stub
	   	
			
			int rs =  (int)sqlSession.insert("AdminMapper.delArtM", adminDto);
		
	}
	
	
	
	@Override
	public  int getDupCheck(String groupid)throws Exception{
		
		int rs =  (Integer)sqlSession.selectOne("AdminMapper.getDupCheck", groupid);
		
		return rs;
	}
	
	
	
	@Override
	public List<AdminDto> getProfile(AdminDto adminDto ) {
		// TODO Auto-generated method stub
		List<AdminDto> result = new ArrayList<AdminDto>();
		result = sqlSession.selectList("AdminMapper.getProfile",adminDto);
		
		return result;
	}
	
	
	


	
}
